package com.ftg.learn.mybatis.learn.mapper;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author kangjx
 * @createTime 2022/7/25
 */
public class StudentMapper {

    /**
     * 就是mybatis最基本的配通 helloworld
     * @return
     */
    public List<Map<String,Object>> showall(){
        //mybatis - jdbc - mysql
        String path = "mybatisconfig.xml";
        SqlSession ss = null;
        try {
            Reader reader = Resources.getResourceAsReader(path);

            SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
            SqlSessionFactory ssf = ssfb.build(reader);
            ss = ssf.openSession();

            List<Map<String,Object>> list = ss.selectList("student.showAllStudent");

            ss.commit();
            return list;
        } catch (IOException e) {
            e.printStackTrace();
            ss.rollback();
        }finally {
            if(Objects.nonNull(ss)){
                ss.close();
            }
        }
        return  null;
    }


}
